package com.wang.dao;

import com.wang.utils.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public abstract class BaseDao {
    private QueryRunner queryRunner = new QueryRunner();

    /***
     * 增删改操作
     * @return 返回影响的行数, 返回-1则执行失败
     */
    public int update(Connection connection, String sql, Object... args) {

        try {
            return queryRunner.update(connection, sql, args);
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }
        return -1;
    }

    public int update(String sql, Object... args) {

        Connection connection = null;

        try {
            connection = JDBCUtils.getConnection();
            return queryRunner.update(connection, sql, args);
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }
        finally {
            JDBCUtils.closeResource(connection);
        }
        return -1;
    }

    /***
     * 查询一条结果
     */
    public <T> T queryForOne(Class<T> clazz, Connection connection, String sql, Object... args) {
        try {
            return queryRunner.query(connection, sql, new BeanHandler<T>(clazz), args);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    public <T> T queryForOne(Class<T> clazz, String sql, Object... args) {

        Connection connection = null;

        try {
            connection = JDBCUtils.getConnection();
            return queryRunner.query(connection, sql, new BeanHandler<T>(clazz), args);
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtils.closeResource(connection);
        }
        return null;
    }

    /***
     * 查询多条结果
     */
    public <T> List<T> queryForList(Class<T> clazz, Connection connection, String sql, Object... args) {
        try {
            return queryRunner.query(connection, sql, new BeanListHandler<T>(clazz), args);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    public <T> List<T> queryForList(Class<T> clazz, String sql, Object... args) {

        Connection connection = null;

        try {
            connection = JDBCUtils.getConnection();
            return queryRunner.query(connection, sql, new BeanListHandler<T>(clazz), args);
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtils.closeResource(connection);
        }
        return null;
    }
    /***
     * 查询特殊值
     */
    public Object queryForValue(Connection connection, String sql, Object... args) {
        try {
            return queryRunner.query(connection, sql, new ScalarHandler(), args);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    public Object queryForValue(String sql, Object... args) {

        Connection connection = null;

        try {
            connection=JDBCUtils.getConnection();
            return queryRunner.query(connection, sql, new ScalarHandler(), args);
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtils.closeResource(connection);
        }
        return null;
    }

    public void closeForeignKeyCheck() {
        Connection connection = null;
        String sql = "set foreign_key_checks = 0";
        try {
            connection=JDBCUtils.getConnection();
            queryRunner.update(connection, sql);
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtils.closeResource(connection);
        }
    }

    public void openForeignKeyCheck() {
        Connection connection = null;
        String sql = "set foreign_key_checks = 1";
        try {
            connection=JDBCUtils.getConnection();
            queryRunner.update(connection, sql);
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtils.closeResource(connection);
        }
    }
}
